package org.xfy.order.controller;

import org.xfy.base.util.JwtUtils;
import org.xfy.base.util.Payload;
import org.xfy.order.domain.Dto.AdoptOrderDto;
import org.xfy.order.service.IOrderAdoptService;
import org.xfy.order.domain.OrderAdopt;
import org.xfy.order.query.OrderAdoptQuery;
import org.xfy.base.util.AjaxResult;
import org.xfy.base.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xfy.org.domain.LoginData;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/orderAdopt")
public class OrderAdoptController {

    @Autowired
    public IOrderAdoptService orderAdoptService;

    @PostMapping("/submitOrder")
    public AjaxResult submitOrder(@RequestBody AdoptOrderDto adoptOrderDto, HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Payload<LoginData> payload = JwtUtils.getdecode(token);
        LoginData loginData = payload.getLoginData();
        return orderAdoptService.submitOrder(adoptOrderDto,loginData);
    }

    /**
     * 接口：添加或修改
     * @param orderAdopt  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderAdopt orderAdopt){ 
        if( orderAdopt.getId()!=null){
		orderAdoptService.update(orderAdopt);
        }else{
                orderAdoptService.add(orderAdopt);
	}
        return  AjaxResult.me();
       
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        orderAdoptService.del(id);
        return AjaxResult.me();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        orderAdoptService.patchDel(ids);
        return AjaxResult.me();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public OrderAdopt findOne(@PathVariable("id")Long id) {
        return orderAdoptService.findOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<OrderAdopt> findAll(){
        return orderAdoptService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/pageList")
    public PageList<OrderAdopt> queryPage(@RequestBody OrderAdoptQuery query) {
        return orderAdoptService.queryPage(query);
    }
}
